package com.xtkj.superclub_tourpal.controller;


import com.xtkj.commons.JsonResult;
import com.xtkj.superclub_entity.entity.tourpal.Team;
import com.xtkj.superclub_entity.entity.tourpal.UserTeam;
import com.xtkj.superclub_entity.entity.user.User;
import com.xtkj.superclub_tourpal.dao.ITourpalTeamDao;
import com.xtkj.superclub_tourpal.service.ITourpalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/tourpal")
public class TourpalController {

    @Autowired
    private ITourpalService tourpalService;
    @Autowired
    private ITourpalTeamDao tourpalTeamDao;


    @RequestMapping(value = "/getTourPal/{userId}", method = RequestMethod.GET)
    public JsonResult getTourPal(@PathVariable("userId") String userId) {
        try {
            return JsonResult.success().setCode(200).setMsg("根据用户查询用户队伍列表成功").setData(tourpalService.getTourPal(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询错误");
        }
    }

    @RequestMapping(value = "/createTourPal", method = RequestMethod.POST)
    public JsonResult createTourPal(@RequestBody Team team, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            tourpalService.createTourPal(userId, team);
            return JsonResult.success().setCode(200).setMsg("创建旅游队队伍成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("创建错误");
        }
    }

    @RequestMapping(value = "/preAddTourPal/{addInfo}", method = RequestMethod.GET)
    public JsonResult preAddTourPal(@PathVariable("addInfo") String addInfo) {
        List<Team> teams = tourpalService.preAddTourPal(addInfo);
        if (CollectionUtils.isEmpty(teams)) {
            return JsonResult.success().setMsg("查询失败，无相关俱乐部").setCode(500);
        }
        return JsonResult.success().setMsg("查出相关的俱乐部列表成功").setCode(200).setData(teams);
    }

    @RequestMapping(value = "/addTourPal", method = RequestMethod.POST)
    public JsonResult addTourPal(@RequestBody Team team, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "262234808388681728";
            tourpalService.addTourPal(userId, team.getTeamId());
            return JsonResult.success().setMsg("加入驴友队成功").setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("加入错误 ");
        }
    }


    @RequestMapping(value = "/outTourPal/{teamId}", method = RequestMethod.DELETE)
    public JsonResult outTourPal(@PathVariable("teamId") String teamId, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "262234808388681728";
            tourpalService.outTourPal(userId, teamId);
            return JsonResult.success().setMsg("退出驴友队成功").setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setMsg("退出驴友队失败").setCode(500);
        }
    }

    @RequestMapping(value = "preAppointViceCaptain/{teamId}", method = RequestMethod.GET)
    public JsonResult preAppointViceCaptain(@PathVariable("teamId") String teamId, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            return tourpalService.preAppointViceCaptain(userId, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询失败");
        }
    }


    @RequestMapping(value = "/appointViceCaptain", method = RequestMethod.PUT)
    public JsonResult appointViceCaptain(@RequestBody UserTeam userTeam, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            tourpalService.appointViceCaptain(userTeam.getTeamId(), userTeam.getUserId());
            return JsonResult.success().setCode(200).setMsg("任命副队长成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("添加失败");
        }
    }

    @RequestMapping(value = "/preCancelViceCaptain/{teamId}", method = RequestMethod.GET)
    public JsonResult preCancelViceCaptain(@PathVariable("teamId") String teamId, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            return tourpalService.preCancelViceCaptain(userId, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询失败");
        }
    }

    @RequestMapping(value = "/cancelViceCaptain", method = RequestMethod.PUT)
    public JsonResult cancelViceCaptain(@RequestBody UserTeam userTeam, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            tourpalService.cancelViceCaptain(userTeam.getUserId(), userTeam.getTeamId());
            return JsonResult.success().setCode(200).setMsg("取消副队长成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("取消失败");
        }
    }

    @RequestMapping(value = "/preAppointLeader/{teamId}", method = RequestMethod.GET)
    public JsonResult preAppointLeader(@PathVariable("teamId") String teamId, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            return tourpalService.preAppointLeader(userId, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询失败");
        }
    }

    @RequestMapping(value = "/appointLeader", method = RequestMethod.PUT)
    public JsonResult appointLeader(@RequestBody UserTeam userTeam, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            tourpalService.appointLeader(userTeam.getUserId(), userTeam.getTeamId());
            return JsonResult.success().setCode(200).setMsg("任命管理员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("任命管理员失败");
        }
    }

    @RequestMapping(value = "/preAssignmentTourPal/{teamId}", method = RequestMethod.GET)
    public JsonResult preAssignmentTourPal(@PathVariable("teamId") String teamId, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "1";
            return tourpalService.preAssignmentTourPal(userId, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("查询失败");
        }
    }

    @RequestMapping(value = "/assignmentTourPal", method = RequestMethod.PUT)
    public JsonResult assignmentTourPal(@RequestBody UserTeam userTeam, HttpServletRequest request) {
        try {
            //String token = request.getParameter("token");
            // String userId = TokenUtil.getUserId(token);
            String userId = "262234808388681728";
            tourpalService.assignmentTourPal(userId, userTeam.getUserId(), userTeam.getTeamId());
            return JsonResult.success().setCode(200).setMsg("转让成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error().setCode(500).setMsg("转让失败");
        }
    }

    @RequestMapping(value = "/getTeamAlbumId/{teamId}", method = RequestMethod.GET)
    public JsonResult getTeam(@PathVariable("teamId") String teamId) {
        Team team = tourpalTeamDao.selectById(teamId);
        return JsonResult.success().setCode(200).setData(team.getTeamAlbumId());
    }


}
